package com.gh.bms;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import com.fetaphon.blelibrary.BMS;
import com.fetaphon.blelibrary.utils.HexUtil;

import androidx.annotation.NonNull;

public class MainActivity extends Activity {

    private Button btn;
    private BMS mBms;

    String mockData = "52 50 42 4C 36 00 01 00 07 0A 00 01 27 00 70 FA 00 00 00 00 00 00 A1 0C 6E 0C 33 00 " +
            "00 00 05 1B 00 22 00 02 1B 00 1B 00 f1 00 00 00 00 00 FF FF 00 00 00 00 00 DF";

    String mockData1 = "52 50 42 4c 16 00 01 00 07 79 00 02 07 00 00 14 14 00 00 00 00 f8";

    String mockData2 = "52 50 42 4C 4F 00 01 00 07 73 00 03 40 00 0C 52 50 5F 48 44 42 4D 53 2D 42 4C 45 14" +
            " 38 39 38 36 30 34 34 38 31 38 31 39 38 30  36 30 39 36 39 37 11 52 50 30 30 39 32 30 32 30 30" +
            " 36 31 35 30 30 30 37 01 30 05 31 2E 37 2E 32 03 31 2E 32 5E";

    String mockData4 = "52 50 42 4C 11 00 01 00 07 76 00 04 02 00 01 01 C7";

    String mockData5 = "52 50 42 4C 5D 00 01 00 07 04 00 05 4E 00 40 19 01 00 A0 09 01 00 E8 03 28 1D 01 00" +
            " A0 09 01 00 D0 07 C0 DA 00 00 90 E2 00 00 E8 03 50 C3 00 00 90 E2 00 00 D0 07 AC 0D 48 0D E8 " +
            "03 42 0E 48 0D D0 07 F0 0A 54 0B E8 03 C4 09 54 0B D0 07 F4 01 2C 01 E8 03 F4 01 48 0D 16 0D " +
            "1E 0F 58";

    String mockData6 = "52 50 42 4C 5D 00 01 00 07 04 00 05 4E 00 40 19 01 00 A0 09 01 00 E8 03 28 1D 01 00" +
            " A0 09 01 00 D0 07 C0 DA 00 00 90 E2 00 00 E8 03 50 C3 00 00 90 E2 00 00 D0 07 AC 0D 48 0D E8 " +
            "03 42 0E 48 0D D0 07 F0 0A 54 0B E8 03 C4 09 54 0B D0 07 F4 01 2C 01 E8 03 F4 01 48 0D 16 0D " +
            "1E 0F 58";

    String mockData7 = "52 50 42 4C 41 00 01 00 07 05 00 06 32 00 20 4E 00 00 50 46 00 00 E8 03 F0 55 00 00" +
            " D0 07 0A 00 48 77 FF FF 00 83 FF FF E8 03 C0 63 FF FF 00 7D 0A 00 B0 3C FF FF 30 75 1E 00 C0 " +
            "F2 FC FF C8 00 BE";

    String mockData8 = "52 50 42 4C 47 00 01 00 07 06 00 07 38 00 3A 37 E8 03 3C 37 D0 07 3A 37 D0 07 3C 37" +
            " B8 0B 03 05 E8 03 00 05 D0 07 F9 FB E8 03 F6 FB D0 07 5A 50 E8 03 5F 50 D0 07 44 41 E8 03 46 " +
            "41 D0 07 F3 F6 E8 03 F1 F6 D0 07 D9";

    String mock =
            "5250424c36000100070a0001270070fa00000000fdffa10c6e0c33000000051b002200021b001b00f10000000000ffff0000000000c9";

//    [5250424c0d0001001900000158, 5250424c0d0001001901000159, 5250424c3f000100077d040330000c52505f4844,
//    424d532d424c4514383938363034343830313139, 37303139383837360130013005312e382e340331, 2e32d6,
//    5250424c16000100077e04020700001414000000, 0001, 5250424c36000100077f04012700ad0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 0700000000000000000000000051,
//    5250424c110001000780040402000100d4, 5250424c5d000100078104054e0040190100880d,
//    01006400281d0100a00901006400c0da000090e2, 0000640020cb0000c0da00006400ac0d480d6400,
//    420e480d6400f00a540b6400280a540b64002c01, 6400f401c800480d800c1e0ff7,
//    5250424c41000100078204063200204e0000983a, 00006400204e0000d0070100b8880000007d0000,
//    6400409c0000f4010a0050c3000064001e00905f, 0100c8006a, 5250424c470001000783040738003a3864003c37,
//    64003c37d007413764000204640000056400f8fa, 6400f6fb640037326400413c6400f8fa6400f6fb,
//    64005a5064005f50640010, 5250424c36000100078404012700ad0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 0700000000000000000000000056,
//    5250424c36000100078504012700ad0101000000, 0000e70cdd0c0a000000251d002200021b001b00,
//    0700000000000000000000000057, 5250424c36000100078604012700ab0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 0000e70cdd0c0a000000251d002200021b001b00,
//    0700000000000000000000000056, 0700000000000000000000000056, 5250424c0d000100190200015a,
//    5250424c0d000100190200015a, 5250424c36000100078704012700ab0101000000,
//    5250424c36000100078704012700ab0101000000, 0000e70cdd0c0a000000251d002200021b001b00,
//    0000e70cdd0c0a000000251d002200021b001b00, 0700000000000000000000000057, 0700000000000000000000000057,
//    5250424c36000100078804012700ab0101000000, 5250424c36000100078804012700ab0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 0700000000000000000000000058,
//    0000e70cdd0c0a000000251d002200021b001b00, 0700000000000000000000000058,
//    5250424c36000100078904012700ad0101000000, 5250424c36000100078904012700ad0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 0000e70cdd0c0a000000251d002200021b001b00,
//    070000000000000000000000005b, 070000000000000000000000005b, 5250424c36000100078a04012700ac0101000000,
//    5250424c36000100078a04012700ac0101000000, 0000e70cdd0c0a000000251d002200021b001b00,
//    0000e70cdd0c0a000000251d002200021b001b00, 070000000000000000000000005b, 070000000000000000000000005b,
//    5250424c36000100078b04012700aa0101000000, 5250424c36000100078b04012700aa0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 0000e70cdd0c0a000000251d002200021b001b00,
//    070000000000000000000000005a, 070000000000000000000000005a, 5250424c36000100078c04012700ac0101000000,
//    5250424c36000100078c04012700ac0101000000, 0000e70cdd0c0a000000251d002200021b001b00,
//    070000000000000000000000005d, 0000e70cdd0c0a000000251d002200021b001b00, 070000000000000000000000005d,
//    5250424c36000100078d04012700ad0101000000, 0000e70cdd0c0a000000251d002200021b001b00,
//    5250424c36000100078d04012700ad0101000000, 0000e70cdd0c0a000000251d002200021b001b00,
//    070000000000000000000000005f, 070000000000000000000000005f, 5250424c36000100078e04012700ad0101000000,
//    5250424c36000100078e04012700ad0101000000, 0000e70cdd0c0a000000251d002200021b001b00,
//    0000e70cdd0c0a000000251d002200021b001b00, 0700000000000000000000000060, 0700000000000000000000000060,
//    5250424c36000100078f04012700ab0101000000, 5250424c36000100078f04012700ab0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 0000e70cdd0c0a000000251d002200021b001b00,
//    070000000000000000000000005f, 070000000000000000000000005f, 5250424c36000100079004012700ad0101000000,
//    5250424c36000100079004012700ad0101000000, 0000e70cdd0c0a000000251d002200021b001b00,
//    0700000000000000000000000062, 0000e70cdd0c0a000000251d002200021b001b00, 0700000000000000000000000062,
//    5250424c36000100079104012700ab0101000000, 5250424c36000100079104012700ab0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 0000e70cdd0c0a000000251d002200021b001b00,
//    0700000000000000000000000061, 0700000000000000000000000061, 5250424c36000100079204012700ad0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 5250424c36000100079204012700ad0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 0700000000000000000000000064, 0700000000000000000000000064,
//    5250424c36000100079304012700ad0101000000, 5250424c36000100079304012700ad0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 0000e70cdd0c0a000000251d002200021b001b00,
//    0700000000000000000000000065, 0700000000000000000000000065, 5250424c36000100079404012700ad0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 5250424c36000100079404012700ad0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 0700000000000000000000000066, 0700000000000000000000000066,
//    5250424c36000100079504012700ab0101000000, 5250424c36000100079504012700ab0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 0700000000000000000000000065,
//    0000e70cdd0c0a000000251d002200021b001b00, 0700000000000000000000000065,
//    5250424c36000100079604012700ab0101000000, 5250424c36000100079604012700ab0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 0000e70cdd0c0a000000251d002200021b001b00,
//    0700000000000000000000000066, 0700000000000000000000000066, 5250424c36000100079704012700ad0101000000,
//    5250424c36000100079704012700ad0101000000, 0000e70cdd0c0a000000251d002200021b001b00,
//    0000e70cdd0c0a000000251d002200021b001b00, 0700000000000000000000000069, 0700000000000000000000000069,
//    5250424c36000100079804012700af0101000000, 5250424c36000100079804012700af0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 0000e70cdd0c0a000000251d002200021b001b00,
//    070000000000000000000000006c, 070000000000000000000000006c, 5250424c36000100079904012700ad0101000000,
//    5250424c36000100079904012700ad0101000000, 0000e80cdd0c0b000000251d002200021b001b00,
//    0000e80cdd0c0b000000251d002200021b001b00, 070000000000000000000000006d, 070000000000000000000000006d,
//    5250424c36000100079a04012700ab0101000000, 5250424c36000100079a04012700ab0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 0000e70cdd0c0a000000251d002200021b001b00,
//    070000000000000000000000006a, 070000000000000000000000006a, 5250424c36000100079b04012700ad0101000000,
//    5250424c36000100079b04012700ad0101000000, 0000e70cdd0c0a000000251d002200021b001b00,
//    070000000000000000000000006d, 0000e70cdd0c0a000000251d002200021b001b00, 070000000000000000000000006d,
//    5250424c36000100079c04012700ac0101000000, 5250424c36000100079c04012700ac0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 0000e70cdd0c0a000000251d002200021b001b00,
//    070000000000000000000000006d, 070000000000000000000000006d, 5250424c0d000100190300015b,
//    5250424c3f000100079d040330000c52505f4844, 424d532d424c4514383938363034343830313139,
//    37303139383837360130013005312e382e340331, 2e32f6, 5250424c16000100079e04020700001414000000, 0021,
//    5250424c36000100079f04012700ad0101000000, 0000e70cdd0c0a000000251d002200021b001b00,
//    0700000000000000000000000071, 5250424c1100010007a0040402000100f4,
//    5250424c5d00010007a104054e0040190100880d, 01006400281d0100a00901006400c0da000090e2,
//    0000640020cb0000c0da00006400ac0d480d6400, 420e480d6400f00a540b6400280a540b64002c01,
//    6400f401c800480d800c1e0f17, 5250424c4100010007a204063200204e0000983a,
//    00006400204e0000d0070100b8880000007d0000, 6400409c0000f4010a0050c3000064001e00905f, 0100c8008a,
//    5250424c4700010007a3040738003a3864003c37, 64003c37d007413764000204640000056400f8fa,
//    6400f6fb640037326400413c6400f8fa6400f6fb, 64005a5064005f50640030,
//    5250424c3600010007a404012700ac0101000000, 0000e70cdd0c0a000000251d002200021b001b00,
//    0700000000000000000000000075, 5250424c3600010007a504012700ab0101000000,
//    0000e70cdd0c0a000000251d002200021b001b00, 0700000000000000000000000075]

    String[] A = {"5250424c0d0001001900000158", "5250424c3f00010007df000330000c52505f4844",
            "424d532d424c4514383938363034343830313139", "37303139383837360130013005312e382e340331", "2e3234"
            , "5250424c1600010007e000020700001414000000", "005f", "5250424c3600010007e100012700ddf90000b8f7"
            , "ffff830c790c0a007e00071d002100021b001b00", "0100000000000000000000000014",
            "5250424c1100010007e200040200010133", "5250424c5d00010007e300054e0040190100880d",
            "01006400281d0100a00901006400c0da000090e2", "0000640020cb0000c0da00006400ac0d480d6400",
            "420e480d6400f00a220b6400280a540b64002c01", "6400f401c800480d800c1e0f23",
            "5250424c4100010007e400063200204e0000983a", "00006400204e0000d0070100b8880000007d0000",
            "6400409c0000f4010a0050c3000064001e00905f", "0100c800c8",
            "5250424c4700010007e5000738003a3864003c37", "64003c37d007413764000204640000056400f8fa",
            "6400f6fb640037326400413c6400f8fa6400f6fb", "64005a5064005f5064006e",
            "5250424c3600010007e700012700dbf900009af7", "ffff830c790c0a007e00071d002100021b001b00",
            "01000000000000000000000000fa", "5250424c3600010007e800012700ddf90000aef7",
            "ffff830c790c0a007e00071d002200021b001b00", "0100000000000000000000000012",
            "5250424c3600010007e900012700dbf90000b8f7", "ffff830c790c0a007e00071d002200021b001b00",
            "010000000000000000000000001b", "5250424c3600010007ea00012700dbf900009af7",
            "ffff830c790c0a007e00071d002200021b001b00", "01000000000000000000000000fe"};

    String[] B = {"5250424c0d0001001900000158", "5250424c3600010007f900012700dff900000000",
            "0000830c790c0a000000061d002200021b001b00", "0100000000000000000000000003",
            "5250424c3600010007fa00012700def900000000", "0000830c790c0a000000061d002200021b001b00",
            "0100000000000000000000000003", "5250424c3600010007fb00012700dcf900000000",
            "0000830c790c0a000000061d002200021b001b00", "0100000000000000000000000002",
            "5250424c3600010007fc00012700def900000000", "0000830c790c0a000000061d002200021b001b00",
            "0100000000000000000000000005", "5250424c3600010007fd00012700def900000000",
            "0000830c790c0a000000061d002200021b001b00", "0100000000000000000000000006",
            "5250424c3600010007fe00012700e0f900000000", "0000830c790c0a000000061d002200021b001b00",
            "0100000000000000000000000009", "5250424c3600010007ff00012700e0f900000000",
            "0000830c790c0a000000061d002200021b001b00", "010000000000000000000000000a",
            "5250424c36000100070001012700e0f900000000", "0000830c790c0a000000061d002200021b001b00",
            "010000000000000000000000000c", "5250424c36000100070101012700def900000000",
            "0000830c790c0a000000061d002200021b001b00", "010000000000000000000000000b",
            "5250424c36000100070201012700dcf900000000", "0000830c790c0a000000061d002200021b001b00",
            "010000000000000000000000000a", "5250424c36000100070301012700def900000000",
            "0000830c790c0a000000061d002200021b001b00", "010000000000000000000000000d",
            "5250424c36000100070401012700e0f900000000", "0000830c790c0a000000061d002200021b001b00",
            "0100000000000000000000000010", "5250424c36000100070501012700e0f900000000",
            "0000830c790c0a000000061d002200021b001b00", "0100000000000000000000000011",
            "5250424c36000100070601012700def900000000", "0000840c790c0b000000061d002200021b001b00",
            "0100000000000000000000000012", "5250424c36000100070701012700def900000000",
            "0000830c790c0a000000061d002200021b001b00", "0100000000000000000000000011",
            "5250424c36000100070801012700def900000000", "0000830c790c0a000000061d002200021b001b00",
            "0100000000000000000000000012", "5250424c36000100070901012700e0f900000000",
            "0000830c790c0a000000061d002200021b001b00", "0100000000000000000000000015",
            "5250424c36000100070a01012700e0f900000000", "0000830c790c0a000000061d002200021b001b00",
            "0100000000000000000000000016", "5250424c36000100070b01012700dff900000000",
            "0000830c790c0a000000061d002200021b001b00", "0100000000000000000000000016",
            "5250424c36000100070c01012700def900000000", "0000830c790c0a000000061d002200021b001b00",
            "0100000000000000000000000016", "5250424c36000100070d01012700e0f900000000",
            "0000830c790c0a000000061d002200021b001b00", "0100000000000000000000000019",
            "5250424c36000100070e01012700e0f900000000", "0000830c790c0a000000061d002200021b001b00",
            "010000000000000000000000001a", "5250424c36000100070f01012700dcf900000000",
            "0000830c790c0a000000061d002200021b001b00", "0100000000000000000000000017",
            "5250424c36000100071001012700e0f900000000", "0000830c790c0a000000061d002200021b001b00",
            "010000000000000000000000001c", "5250424c36000100071101012700def900000000",
            "0000830c790c0a000000061d002200021b001b00", "010000000000000000000000001b",
            "5250424c36000100071201012700def900000000", "0000830c790c0a000000061d002200021b001b00",
            "010000000000000000000000001c", "5250424c36000100071301012700e0f900000000",
            "0000830c790c0a000000061d002200021b001b00", "010000000000000000000000001f",
            "5250424c36000100071401012700def900000000", "0000830c790c0a000000061d002200021b001b00",
            "010000000000000000000000001e", "5250424c36000100071501012700e0f900000000",
            "0000830c790c0a000000061d002200021b001b00", "0100000000000000000000000021",
            "5250424c36000100071601012700e0f900000000", "0000830c790c0a000000061d002200021b001b00",
            "0100000000000000000000000022", "5250424c36000100071701012700e1f900000000",
            "0000830c790c0a000000061d002200021b001b00", "0100000000000000000000000024",
            "5250424c36000100071801012700def900000000", "0000830c790c0a000000061d002200021b001b00",
            "0100000000000000000000000022", "5250424c36000100071901012700e1f900000000",
            "0000830c790c0a000000061d002200021b001b00", "0100000000000000000000000026",
            "5250424c36000100071a01012700e0f900000000", "0000830c790c0a000000061d002200021b001b00",
            "0100000000000000000000000026", "5250424c36000100071b01012700e0f900000000",
            "0000830c790c0a000000061d002200021b001b00", "0100000000000000000000000027",
            "5250424c36000100071c01012700e1f900000000", "0000830c790c0a000000061d002200021b001b00",
            "0100000000000000000000000029", "5250424c36000100071d01012700e1f900000000",
            "0000830c790c0a000000061d002200021b001b00", "010000000000000000000000002a",
            "5250424c36000100071e01012700e1f900000000", "0000830c790c0a000000061d002200021b001b00",
            "010000000000000000000000002b", "5250424c36000100071f01012700e0f900000000",
            "0000830c790c0a000000061d002200021b001b00", "010000000000000000000000002b",
            "5250424c36000100072001012700dff900000000", "0000830c790c0a000000061d002200021b001b00",
            "010000000000000000000000002b", "5250424c36000100072101012700e1f900000000",
            "0000830c790c0a000000061d002200021b001b00", "010000000000000000000000002e",
            "5250424c36000100072201012700e1f900000000", "0000830c790c0a000000061d002200021b001b00",
            "010000000000000000000000002f", "5250424c36000100072301012700dff900000000",
            "0000830c790c0a000000061d002200021b001b00", "010000000000000000000000002e",
            "5250424c36000100072401012700e1f900000000", "0000830c790c0a000000061d002200021b001b00",
            "0100000000000000000000000031", "5250424c36000100072501012700e0f900000000",
            "0000830c790c0a000000061d002200021b001b00", "0100000000000000000000000031"};
    private Handler mHandler;
    private int i;

    private String test = "5250424c36000100070b01012700dff9000000000000830c790c0a000000061d002200021b001b000100000000000000000000000016";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = findViewById(R.id.btn);

        mBms = BMS.getInstance();
        mBms.init(this);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                String trim = test.replace(" ", "");
//                byte[] bytes = HexUtil.hexStringToBytes(trim);
//                mBms.parse(bytes);
                mHandler.sendEmptyMessage(1);

            }
        });

        mHandler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                if (i > B.length - 2) return;
                String trim = B[i].replace(" ", "");
                byte[] bytes = HexUtil.hexStringToBytes(trim);
                mBms.parse(bytes);
                i++;
                mHandler.sendEmptyMessageDelayed(1, 300);
            }
        };
    }

}